# graph.py

from __future__ import division

from PIL import Image, ImageDraw

from ot_simulator import base_graph

Max_dimension = 300

class graph(base_graph.base_graph):
    '''This graphs multiple sequences of points, possibly in different colors.

    First, call :meth:`plot` for each sequence of points.

    Then, call :meth:`show` to display the graph, or :meth:`write` to write it
    to an image file.

    Call :meth:`reset` to start all over again.

    Does not draw axes.  :-(
    '''

    def __init__(self, max_dimension = Max_dimension):
        base_graph.base_graph.__init__(self)
        self.max_dimension = max_dimension

    def get_width(self):
        return self.max_dimension

    def get_height(self):
        return self.max_dimension

    def make_image(self):
        scale, max_x, max_y = self.calc_scale()

        size_x = max(30, int(2 * max_x * scale + 1))
        size_y = max(30, int(2 * max_y * scale + 1))
        #print "max_x:", max_x, ", max_y:", max_y, ", len(self.points):", len(self.points)
        #print "scale:", scale, ", size:", (size_x, size_y)
        image = Image.new("RGB", (size_x, size_y))
        drawer = ImageDraw.Draw(image)
        #drawer.line([(0, 0), (2*max_x*scale, 2*max_y*scale)], fill=255)
        for color, points in self.points:
            drawer.line(self.scale_points(points), fill=color)
        return image

    def show(self):
        self.make_image().show()

    def write(self, filename):
        self.make_image().save(filename)

